require "Common/define"
require "Logic/Config"
require "Common/functions"

RedDotsHeroHandbookRewardHandler ={};
local this = RedDotsHeroHandbookRewardHandler;

function RedDotsHeroHandbookRewardHandler.init()
    CloseEvent(Config.EventType.Fresh_Handbook,this.update);
    ListenEvent(Config.EventType.Fresh_Handbook,this.update);

    return this;
end

--参数1：指定英雄Id，nil代表检查所有英雄
---英雄详情新英雄奖励红点
function RedDotsHeroHandbookRewardHandler.getFlag(...)
    local flag = Config.RedDotsFlagState.None;
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.HandBook,false) then
        local partnerId = ...;
        local heroList = nil;
        if partnerId~=nil then
            heroList ={};
            local hero = logicMgr.HeroManager.getHeroDatabyPartnerId(partnerId,true);
            if hero~=nil then
                table.insert(heroList,hero);
            end
        else
            heroList = logicMgr.HeroManager.GetAllHero(true);
        end
        for i = 1, #heroList do
            flag = this.getFlagByHero(heroList[i]);
            if flag == Config.RedDotsFlagState.Needful then
                break;
            end
        end
    end
    return flag;
end

function RedDotsHeroHandbookRewardHandler.getFlagByHero(hero)
    local info = logicMgr.HeroManager.getHandbookByModelId(hero.partnerModelId,true);
    if info and not info.isReward then
        return Config.RedDotsFlagState.Needful
    end
    return Config.RedDotsFlagState.None;
end

function RedDotsHeroHandbookRewardHandler.update()
    RedDotsManager.addBroadcast(Config.RedDotsType.HeroHandbookReward);
end